package com.jy.wms.service;

import com.jy.wms.common.UUIDUtils;
import com.jy.wms.common.annotions.BaseServiceDescribe;
import com.jy.wms.common.base.BaseService;
import com.jy.wms.common.base.ServiceException;
import com.jy.wms.common.base.SetAndModifyModel;
import com.jy.wms.common.base.SucessException;
import com.jy.wms.pojo.WmsCustomerFileWarehouse;
import com.jy.wms.dao.WmsCustomerFileWarehouseMapper;
import com.sun.corba.se.spi.ior.ObjectKey;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import org.apache.xmlbeans.impl.xb.xsdschema.Public;
import org.springframework.stereotype.Service;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
    * @ClassName: WmsCustomerFileWarehouseService.java
    * @Description: TODO(这里用一句话描述这个类的作用)
    * @Author Liyi
    * @Date Fri Oct 13 09:52:56 CST 2017
    */

    @Service
    public class WmsCustomerFileWarehouseService extends BaseService<WmsCustomerFileWarehouseMapper,WmsCustomerFileWarehouse> {

     /**
     * 插入一条记录
     * @param setAndModifyModel
     * @return int
     */
     @BaseServiceDescribe(moduleName = "客户档案仓库管理",btnName = "新增客户档案仓库信息")
    public void baseInsert(SetAndModifyModel setAndModifyModel) throws SucessException,ServiceException{
        List<String> list = setAndModifyModel.getIds();
        List<WmsCustomerFileWarehouse> data = new ArrayList<WmsCustomerFileWarehouse>();
        WmsCustomerFileWarehouse wmsCustomerFileWarehouse = new WmsCustomerFileWarehouse();
        for (String s : list) {
            wmsCustomerFileWarehouse.setId(UUIDUtils.uuid());
            wmsCustomerFileWarehouse.setSfyx("1");
            wmsCustomerFileWarehouse.setCkid(s);
            wmsCustomerFileWarehouse.setSskhid(setAndModifyModel.getId());
            data.add(wmsCustomerFileWarehouse);
        }
        Integer addCount = dao.baseInsertAll(data);
        if(addCount>0){
            throw new SucessException("新增客户档案仓库信息成功");
        }else{
            throw new ServiceException("新增客户档案仓库信息失败，未插入数据库");
        }
    }

   /**
    * 插入一条记录（匹配有值的字段）
    * @param entity 实体对象
    * @return int
    */
    public Integer baseInsertSelective( WmsCustomerFileWarehouse entity){
         return dao.baseInsertSelective( entity);
    }

   /**
    * 删除（根据主键ID删除）
    * @param id 主键
    * @return int
    */
    public Integer baseDeleteByPrimaryKey( String id){
         return dao.baseDeleteByPrimaryKey( id);
    }

    /**
     * 修改多条记录（根据主键ID删除）
     * @param sskhid 所属客户ID
     * @return int
     */
    @BaseServiceDescribe(moduleName = "客户档案仓库管理",btnName = "修改客户档案仓库信息")
    public void editBySskhid( String sskhid,SetAndModifyModel setAndModifyModel)throws SucessException,ServiceException{
        dao.baseDeleteBySskhid(sskhid);
        List<String> list = setAndModifyModel.getIds();
        List<WmsCustomerFileWarehouse> data = new ArrayList<WmsCustomerFileWarehouse>();
        WmsCustomerFileWarehouse wmsCustomerFileWarehouse = new WmsCustomerFileWarehouse();
        for (String s : list) {
            wmsCustomerFileWarehouse.setId(UUIDUtils.uuid());
            wmsCustomerFileWarehouse.setSfyx("1");
            wmsCustomerFileWarehouse.setCkid(s);
            wmsCustomerFileWarehouse.setSskhid(setAndModifyModel.getId());
            data.add(wmsCustomerFileWarehouse);
        }
        Integer addCount = dao.baseInsertAll(data);
        if(addCount>0){
            throw new SucessException("修改客户档案仓库信息成功");
        }else{
            throw new ServiceException("修改客户档案仓库信息失败");
        }
    }

   /**
    * 根据省和城市选择路线
    * @param idList 主键ID列表
    * @return int
    */
    public Integer baseDeleteBathPrimaryKeys( List<String> idList){
         return dao.baseDeleteBathPrimaryKeys( idList );
    }

    @BaseServiceDescribe(moduleName = "客户档案仓库管理",btnName = "根据省和城市选择路线")
    public List<Map<String,Object>> getLineByDz(WmsCustomerFileWarehouse entity){
        return dao.getLineByDz(entity);
    }

    /**
    * 分页查询
    * @param entity 实体
    * @return Page<WmsCustomerFileWarehouse>
    */
    public Page<WmsCustomerFileWarehouse> baseSelectPage(WmsCustomerFileWarehouse entity ){
        PageHelper.startPage(entity.getPage(),entity.getRows());
        return (Page<WmsCustomerFileWarehouse>)dao.baseSelectPage( entity );
    }

   /**
    * 查询（根据主键ID查询）
    * @param id 主键ID
    * @return WmsCustomerFileWarehouse
    */
    public WmsCustomerFileWarehouse baseSelectByPrimaryKey( String id ){
         return dao.baseSelectByPrimaryKey( id );
    }

   /**
    * 查询（根据ID 批量查询）
    * @param idList 主键ID列表
    * @return list
    */
    public Integer baseSelectBathPrimaryKeys( List<WmsCustomerFileWarehouse> idList ){
         return dao.baseSelectBathPrimaryKeys( idList );
    }

   /**
    *修改 （匹配有值的字段）
    * @param entity 主键ID列表
    * @return int
    */
    public Integer baseUpdateByPrimaryKeySelective( WmsCustomerFileWarehouse entity){
         return dao.baseUpdateByPrimaryKeySelective( entity);
    }

   /**
    *修改（根据主键ID修改）
    * @param entity 主键ID列表
    * @return int
    */
    public Integer baseUpdateByPrimaryKey( WmsCustomerFileWarehouse entity){
         return dao.baseUpdateByPrimaryKey( entity);
    }

    /**
     * 根据客户ID查询所有客户拥有的仓库ID
     * @param sskhid 所属客户ID
     * @return List<WmsCustomerFileWarehouse>
     */
    public List<String> getAllBySskhid(String sskhid) throws Exception{
        try {
            return dao.getAllBySskhid(sskhid);
        } catch (Exception e) {
            throw new ServiceException("查询客户档案仓库的ids",e);
        }
    }
}
